package com.yunhe.sms.control;

import com.yunhe.sms.dao.UserDao;
import com.yunhe.sms.mode.User;
import com.yunhe.sms.view.user.UserAdd;
import com.yunhe.sms.view.user.UserDle;
import com.yunhe.sms.view.user.UserList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制
 */
public class UserController {
    /**
     * 用户管理路由
     */
    public static void userRoute(){
        List<User> userList = UserDao.getAllUser();
        int i = UserList.userListUI(userList);
        switch (i){
            case 1:{
                try{
                    User user = UserAdd.userAddUI();//添加用户
                    UserDao.userInsert(user);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
                userRoute();
                break;
            }
            case 2:{
                User user = UserDle.userDleUI();//删除用户
                UserDao.userDle(user);
                userRoute();
                break;
            }
            case 3:{
                try {
                    User user = UserAdd.userAddUI();
                    UserDao.userUpdate(user);// 修改用户
                    userRoute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case 4:HomeControllerFactory.homeRoute(strTypeToInt(LoginController.getCurrUser().getType()));break;//返回主页
            case 5:userRoute();//刷新
        }
    }

    /**
     * 转换用户类型
     * @param type 字符
     * @return 返回1/2
     */
    public static int strTypeToInt(String type){
        int i = 0;
        switch (type){
            case User.TYPE_TEACHER_STR: i = 1;break;
            case User.TYPE_ADMIN_STR: i = 2;break;
        }
        return i;
    }

    /**
     * 用户数字类型转换成字符类型
     * @param type
     * @return
     */
    public static String intTypeto2Str(int type){
        String str = null;
        switch (type){
            case User.TYPE_TEACHER_INT:str = User.TYPE_TEACHER_STR;break;
            case User.TYPE_ADMIN_INT:str = User.TYPE_ADMIN_STR;break;
        }
        return str;
    }
}
